/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import java.io.InputStream;
import java.util.Properties;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

/**
 *
 * @author AdminNUS
 */
@ManagedBean(name="displayPDFBean")
//@Named(value = "displayPDFBean")
@RequestScoped
public class DisplayPDFBean {
    @Resource(name = "macteamData")
    private DataSource macteamData;

    /** Creates a new instance of DisplayPDFBean */
    public DisplayPDFBean() {
    }

    public void exportReportToPdf() throws JRException, FileNotFoundException, IOException {

        Connection conn = null;
        JasperReport jasperreport;
        JasperPrint jasperprint;

        try {
            conn = macteamData.getConnection();
        } catch (SQLException ex) {
            Logger.getLogger(ExportToPDFBean.class.getName()).log(Level.SEVERE, null, ex);
        }
 

 //System.out.println("********" + FacesContext.getCurrentInstance().getExternalContext().
 HashMap jasperParameter = new HashMap();
        JasperDesign jd1;

            jd1 = JRXmlLoader.load("F:\\th44\\trunk\\MacTeam\\MacTeam-war\\web\\jasperreports\\report1.jasper");
 JasperReport   jasperReport = JasperCompileManager.compileReport(jd1);

       //JasperDesign jasperDesign = JRXmlLoader.load("F:/th44/trunk/MacTeam/MacTeam-war/jasperreports/report1.jasper");
//JasperReport jasperReport = JasperCompileManager.compileReport("F:/th44/trunk/MacTeam/MacTeam-war/web/jasperreports/report1.jasper"); 


        //jasperreport = JasperCompileManager.compileReport(System.getProperty("user.home")+"/Documents/NetBeansProjects/trunk/MacTeam/MacTeam-war/web/jasperreports/salesLeadReport.jrxml");

        //jasperprint = JasperFillManager.fillReport(jasperreport, jasperParameter, conn);

        //JasperExportManager.exportReportToPdfFile(jasperprint, System.getProperty("user.home")+"/Documents/NetBeansProjects/trunk/MacTeam/MacTeam-war/web/jasperreports/testReport.pdf");

        //New Code
         InputStream reportStream = ((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true)).getServletContext().getResourceAsStream("/jasperreports/report1.jasper");
   
        JasperPrint  jasperPrint = JasperFillManager.fillReport(jasperReport, jasperParameter, conn);
        //JasperExportManager.exportReportToPdfFile(jasperPrint, System.getProperty("user.home")+"/Documents/NetBeansProjects/trunk/trunk/MacTeam/dist/gfdeploy/MacTeam/MacTeam-war_war/jasperreports/testConvertedCustomerReport2.pdf");
     
        JasperExportManager.exportReportToPdfFile(jasperPrint,"/jasperreports/report1.pdf");
       
        FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "/jasperreports/testConvertedCustomerReport2.pdf?faces-redirect=true");
        //FacesContext.getCurrentInstance().getExternalContext().redirect("/jasperreports/testConvertedCustomerReport2.pdf");
        //FacesContext.getCurrentInstance().getExternalContext().dispatch("/jasperreports/testConvertedCustomerReport2.pdf");
        //FacesContext.getCurrentInstance().getExternalContext().redirect("/jasperreports/testConvertedCustomerReport2.pdf");
        
        showPDF(); 
                
    }
    
    public void showPDF() {
        
        try
{
            String strSQL = "SELECT vendor.`COMPANYNAME` AS COMPANYNAME, vendor.`BANKACCOUNT` AS BANKACCOUNT, vendor.`STAR` AS STAR  FROM `vendor` vendor";
            
            ResultSet rsEvents = macteamData.getConnection().prepareStatement(strSQL).executeQuery();
            JRResultSetDataSource jrResultSetDataSource = new JRResultSetDataSource(rsEvents);

            //InputStream reportStream = ((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true)).getServletContext().getResourceAsStream(null);
            InputStream reportStream = ((HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true)).getServletContext().getResourceAsStream("/jasperreports/report1.jasper");
            
            ((HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse()).setContentType("application/pdf");     
            ServletOutputStream outputStream = ((HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse()).getOutputStream();
            HashMap jasperParameter = new HashMap();
        
            JasperRunManager.runReportToPdfStream(reportStream, outputStream, jasperParameter, jrResultSetDataSource);
            

            outputStream.flush();

            outputStream.close();
            
            }

        catch (JRException jrex)

        {

            System.out.println("********** Jasperreport Exception");

            jrex.printStackTrace();

        }

        catch (Exception ex)

        {

            ex.printStackTrace();

        }
}
    
    
}
